Infrastructure as Code: Deploy CloudFormation Stack
We'll cover the following
Deploying#
Now it’s time to deploy our infrastructure. Let’s run the deploy-infra.sh
command. We can check the status of our stack from the CloudFormation console. The events tab shows which resources are being created, modified, or destroyed.
When successful, the script should show us the URL for reaching our application.
And now we can test that our application is up and running with curl
.
Now we can commit our infrastructure code to GitHub to checkpoint our progress.
Github access token#
NOTE: Before we run the code, we will require a Github access token so you don’t need to put in your username and password whenever you access Github through the terminal widget. To generate an access token, go to https://github.com/settings/tokens/new and click Generate new token. Give it repo and admin:repo_hook permissions, and click Generate token.
NOTE: You just have to put your Github token ONCE in the terminal widget, it will persist that information in all the other terminal widgets automatically.
Run and push the code#
All of the files have already been placed. You just have to run the code.
NOTE: It will automatically push the code to your Github too.
NOTE: In
deploy-infra.sh
file, on Line #1 (which is also highlighted in the code), another file by the name ofaws_credentials
is getting executed to set your AWS credentials but you can’t see it. Similarly ingithub.sh
, on Line #1,github_credentials.sh
is getting executed to set your Github credentials. We have hidden these two files so your main focus is on the code, not to set up the credentials.
/
- deploy-infra.sh
We now have our application running in the cloud, with its basic infrastructure managed through code. However, if we make a change to our application, our EC2 instance won’t be updated.
🔍 Only one action at a time can be in progress for a given CloudFormation stack. If you get an error that says your stack “is in [?] state and cannot be updated”, then wait until the stack has finished its current update and try again.
⚠️ If there is an error with the creation of your stack, you may get a message saying that your stack “is in ROLLBACK_COMPLETE state and cannot be updated.” When this happens, you will not be able to deploy again. CloudFormation does this to give you a chance to inspect the error that caused the deployment to fail. Once you’ve addressed the issue, you’ll need to delete the stack and redeploy it.
In order to get a pictorial view of our developed CloudFormation stack so far, below is the design view which shows the resources we created and their relationships.
In the next lesson, we will make our instance receive a new version of our application automatically as soon as a change is pushed to GitHub.